<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @filename admin_news.php
 * 公告管理
 * @author hmw
 *
 */

class Admin_news extends MY_Controller {

    function Admin_news() {
        parent::MY_Controller();
    }
    
    /**
     * 默认页面
     * @return unknown_type
     */
    function index(){
    	self::news_list();
    }
    
    /**
     * 显示新闻公告列表
     * @return null
     */
    function news_list(){
    	$this->config->load('setup');
    	$types = $this->config->item('news_type');
    	$this->load->model('mnews');
    	$news = $this->mnews->get_news_list();
    	$this->template->view('admin/news_list',array('news'=>$news,'types'=>$types));
    }
    
    /**
     * 添加公告页面
     * @return unknown_type
     */
    function news_new(){
    	$this->config->load('setup');
    	$types = $this->config->item('news_type');
    	$colors = $this->config->item('news_title_colors');
    	$this->template->view('admin/news_add',array('types'=>$types,'colors'=>$colors));
    }
    
    /**
     * 处理添加公告
     * @return unknown_type
     */
    function news_add(){
    	$news_title = $this->input->post('news_title');
    	$news_content = $this->input->post('news_content');
    	if(!$news_title || !$news_content){
    		echo '公告标题和内容都必须填写！';exit;
    	}
    	$news_brief = $this->input->post('news_brief');
    	$news_from = $this->input->post('news_from');
    	$news_type = $this->input->post('news_type');
    	$news_link = $this->input->post('news_link');
    	$color = $this->input->post('color');
    	$is_bold = $this->input->post('is_bold') ? $this->input->post('is_bold') : 0;
    	$news_time = date('Y-m-d H:i:s');
    	$values = array('news_title'=>$news_title,'news_content'=>$news_content,'news_type'=>$news_type,'news_time'=>$news_time,'news_from'=>$news_from,'news_brief'=>$news_brief,'news_state'=>1,'news_link'=>$news_link,'color'=>$color,'is_bold'=>$is_bold);
    	$this->load->model('mnews');
    	if($this->mnews->insert_news($values)){
    		echo '添加成功！&nbsp;<a href="/admin_news/news_new/">继续添加</a>&nbsp;&nbsp;<a href="/admin_news/news_list/">查看列表</a><br />';
    		echo '标题：'.$news_title.'<br />';
    		echo '正文：<br />';
    		echo $news_content;
    	} else {
    		echo '添加失败！请重试！';
    	}
    }
    
    /**
     * 公告修改页面
     * @param $id
     * @return unknown_type
     */
    function news_edit($id=''){
    	if($id === ''){
    		echo '请选中公告后再继续操作！';exit;
    	}
    	$this->config->load('setup');
    	$types = $this->config->item('news_type');
    	$colors = $this->config->item('news_title_colors');
    	$this->load->model('mnews');
    	$info = $this->mnews->get_news_detail($id);
    	$this->template->view('admin/news_edit',array('info'=>$info,'types'=>$types,'colors'=>$colors));
    }
    
    /**
     * 处理公告修改
     * @return unknown_type
     */
    function news_modify(){
    	$id = $this->input->post('id');
    	$news_title = $this->input->post('news_title');
    	$news_content = $this->input->post('news_content');
    	if(!$news_title || !$news_content){
    		echo '公告标题和内容都必须填写！';exit;
    	}
    	$news_brief = $this->input->post('news_brief');
    	$news_from = $this->input->post('news_from');
    	$news_type = $this->input->post('news_type');
    	$news_link = $this->input->post('news_link');
    	$color = $this->input->post('color');
    	$is_bold = $this->input->post('is_bold') ? $this->input->post('is_bold') : 0;
    	$news_time = date('Y-m-d H:i:s');
    	$values = $values = array('news_title'=>$news_title,'news_content'=>$news_content,'news_type'=>$news_type,'news_time'=>$news_time,'news_from'=>$news_from,'news_brief'=>$news_brief,'news_link'=>$news_link,'color'=>$color,'is_bold'=>$is_bold);
    	$this->load->model('mnews');
    	if($this->mnews->update_news($id,$values)){
    		echo '公告修改成功！&nbsp;<a href="/admin_news/news_new/">添加新公告</a>&nbsp;&nbsp;<a href="/admin_news/news_list/">查看列表</a><br />';
    		echo '标题：'.$news_title.'<br />';
    		echo '正文：<br />';
    		echo $news_content;
    	} else {
    		echo '公告修改失败！请重试！';
    	}
    }
    
    /**
     * 删除公告
     * @param $id
     * @return unknown_type
     */
    function news_del($id){
    	if($id === ''){
    		echo '请选中公告后再继续操作！';exit;
    	}
    	$this->load->model('mnews');
    	$values = array('news_state'=>0);
    	if($this->mnews->update_news($id,$values)){
    		echo '公告删除成功！&nbsp;<a href="/admin_news/news_new/">添加新公告</a>&nbsp;&nbsp;<a href="/admin_news/news_list/">查看列表</a><br />';
    	} else {
    		echo '公告删除失败！请重试！';
    	}
    }


}

?>